package com.qidian.zl.paper.test;

import com.qidian.zl.paper.factory.ServiceFactory;
import com.qidian.zl.paper.pojo.Paper;
import com.qidian.zl.paper.service.PaperService;
import com.qidian.zl.paper.util.DateUtils;
import com.qidian.zl.paper.util.JsonTools;
import com.qidian.zl.paper.util.StResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebServlet("/savePaper")
public class PaperAddServlet extends HttpServlet {
    private PaperService paperService = ServiceFactory.getPaperService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StResult result = null;
        Long paperId = -1L;
        try {
            paperId = new Long(request.getParameter("paperId"));
        } catch (Exception e) {
        }
        String paperTitle = request.getParameter("paperTitle");
        String paperDesc = request.getParameter("paperDesc");
        if (paperTitle == null || "".equals(paperTitle)) {
            result = StResult.fail("问卷标题不能为空");
        } else {
            Paper paper = new Paper();
            paper.setPaperId(paperId);
            paper.setPaperTitle(paperTitle);
            paper.setPaperDesc(paperDesc);
            paper.setPaperCreated(DateUtils.getNow());
           // System.out.println(paper.toString());
            paperId = paperService.saveOrUpdate(paper);

            if (paperId == -1) {
                result = StResult.fail("保存或修改失败");
            } else {
                result = StResult.ok(paperId + "");
            }
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.obj2String(result));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
